package com.oocl.euc.core.maintenance.controller;

import com.oocl.euc.common.constant.StatusType;
import com.oocl.euc.core.builder.StatusBuilder;
import com.oocl.euc.core.common.ITTestBase;
import com.oocl.euc.core.maintenance.model.Status;
import com.oocl.euc.core.maintenance.repository.StatusRepository;
import org.junit.After;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

public class StatusControllerIT extends ITTestBase {

    @Autowired
    StatusRepository statusRepository;

    @Autowired
    StatusBuilder statusBuilder;

    @After
    public void reset() {
        statusRepository.deleteAll();
    }

    @Test
    public void should_return_task_status_when_given_task_constant() throws Exception {
        Status taskSatatus = statusBuilder.defaultTaskStatusBuilder().saveToDB();
        mockMvc.perform(get("/rest/status/findStatus")
                .param("statusType", StatusType.STATUS_TYPE_TASK))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$[0].statusName").value(taskSatatus.getStatusName()));
        statusRepository.delete(taskSatatus);
    }

    @Test
    public void should_return_overall_status_when_given_overall_constant() throws Exception {
        Status overAllStatus = statusBuilder.defaultOverallStatusBuilder().saveToDB();
        mockMvc.perform(get("/rest/status/findStatus")
                .param("statusType", StatusType.STATUS_TYPE_OVERALL))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$[0].statusName").value(overAllStatus.getStatusName()));
        statusRepository.delete(overAllStatus);
    }
}
